package gui;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import control.ExitControl.ExitListener;
import control.ForcedMotion.MovieListener;
import control.ForcedMotion.IdleListener;
import control.ForcedMotion.MovieAutoListener;
import control.ForcedSync.AsyncListener;
import control.ForcedSync.SyncListener;
import util.Picture;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import java.net.InetSocketAddress;
import java.util.HashMap;

public class CameraPanel extends JPanel {
	private ImagePanel panel1;
	private ImagePanel panel2;
	private JRadioButton btnSync;
	private JRadioButton btnAsync;
	private JRadioButton btnMovie;
	private JRadioButton btnIdle;
	private JButton exitButton;
	private JRadioButton rdbtnMotion;
	private JLabel syncMode;
	private JLabel speedMode;
	private JLabel delayText1;
	private JLabel delayText2;
	private JRadioButton autoMovie;

	public CameraPanel() {
		setSize(638, 373);
		int xOffset = 360;
		Font font = new Font("Times New Roman", Font.PLAIN, 14);

		// Sync
		syncMode = new JLabel("Mode: Sync");
		syncMode.setFont(font);

		// idle or movie
		speedMode = new JLabel("Mode: Idle");
		speedMode.setFont(font);

		JPanel panel = new JPanel();

		panel2 = new ImagePanel();

		btnAsync = new JRadioButton("Asynchronous");

		btnMovie = new JRadioButton("Movie");

		btnIdle = new JRadioButton("Idle");			

		exitButton = new JButton("Exit");

		btnSync = new JRadioButton("Synchronous");
		btnSync.setSelected(true);

		delayText1 = new JLabel("ms");

		delayText2 = new JLabel("ms");
		
		autoMovie = new JRadioButton("Auto");
		autoMovie.setSelected(true);
		
		ButtonGroup bgSync = new ButtonGroup();
		bgSync.add(btnAsync);
		bgSync.add(btnSync);
		
		ButtonGroup bgMotion = new ButtonGroup();
		bgMotion.add(btnMovie);
		bgMotion.add(btnIdle);
		bgMotion.add(autoMovie);
		map = new HashMap<InetSocketAddress, Integer>();
		

		

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(panel, GroupLayout.PREFERRED_SIZE, 700, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(delayText2)
									.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(delayText1))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(34)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(speedMode)
								.addComponent(syncMode))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(btnSync)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnAsync)
									.addPreferredGap(ComponentPlacement.RELATED, 190, Short.MAX_VALUE)
									.addComponent(exitButton))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(btnMovie)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnIdle)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(autoMovie)))))
					.addContainerGap(62, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 203, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(delayText2)
						.addComponent(delayText1))
					.addGap(37)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnMovie)
						.addComponent(btnIdle)
						.addComponent(autoMovie)
						.addComponent(speedMode))
					.addGap(32)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(exitButton)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnAsync)
							.addComponent(btnSync)
							.addComponent(syncMode)))
					.addGap(38))
		);

		panel1 = new ImagePanel();
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup().addComponent(panel2,
						GroupLayout.PREFERRED_SIZE, 320,
						GroupLayout.PREFERRED_SIZE).addGap(50).addComponent(
						panel1, GroupLayout.PREFERRED_SIZE, 320,
						GroupLayout.PREFERRED_SIZE).addGap(5)));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup().addGroup(
						gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(panel2,
										GroupLayout.PREFERRED_SIZE, 240,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(panel1,
										GroupLayout.PREFERRED_SIZE, 240,
										GroupLayout.PREFERRED_SIZE))
						.addContainerGap(GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		panel.setLayout(gl_panel);
		setLayout(groupLayout);

	}

	private int delayCount1;
	private int delayCount2;
	private long delay1;
	private long delay2;
	private HashMap<InetSocketAddress, Integer> map;
	private int cameras;

	public void displayPicture(Picture p) {
		if (p.getSync())
			this.syncMode.setText("Mode: Sync");
		else
			this.syncMode.setText("Mode: Async");
		if(!map.containsKey(p.getAddr())){
			map.put(p.getAddr(), cameras);
			cameras++;
		}
		int camera = map.get(p.getAddr());	
		if (camera == 0) {
			panel1.refresh(p.getJpeg());
			delay1 += p.getDelay();
			delayCount1++;
			if (delayCount1 == 10 || !motion) {
				delayText1.setText(p.getHostName() + " --- " +Long.toString(delay1 / delayCount1) + " ms"); // medelvärde
				delayCount1 = 0;
				delay1 = 0;
			}
		} else if (camera == 1) {
			panel2.refresh(p.getJpeg());
			delay2 += p.getDelay();
			delayCount2++;
			if (delayCount2 == 10 || !motion) {
				delayText2.setText(p.getHostName() + " --- " + Long.toString(delay2 / delayCount2)+ " ms");
				delayCount2 = 0;
				delay2 = 0;
			}
		}

	}

	public void addSyncListener(SyncListener list) {
		btnSync.addActionListener(list);
	}

	public void addAsyncListener(AsyncListener list) {
		btnAsync.addActionListener(list);
	}

	public void addMotionListener(MovieListener list) {
		btnMovie.addActionListener(list);
	}

	public void addNoMotionListener(IdleListener motionListener2) {
		btnIdle.addActionListener(motionListener2);
	}

	public void addExitListener(ExitListener list) {
		exitButton.addActionListener(list);
	}
	
	public void addMovieAutoListener(MovieAutoListener auto){
		autoMovie.addActionListener(auto);
	}
	
	public void reset(){
		autoMovie.setSelected(true);
		btnSync.setSelected(true);
		map = new HashMap<InetSocketAddress, Integer>();
		cameras = 0;
	}


	private boolean motion;

	public void setMotion(boolean motion, String user) {
		this.motion = motion;
		if (motion)
			speedMode.setText(user + "Movie ");
		else
			speedMode.setText(user + "Idle ");
	}
}
